<?php
/**
 * Created by PhpStorm.
 * User: 六花
 * Date: 2019/5/16
 * Time: 16:17
 */

class recommend_settingApp extends BackendApp{


    var $_recommend_mod;

    function __construct()
    {
        $this->recommend_settingApp();
    }

    function recommend_settingApp()
    {
        parent::BackendApp();
        $this->_recommend_mod =& m('recommend_setting');
        $this->_goods_mod =& m('goods');
    }

    function index()
    {
        $recommend_model =  & m('recommend_setting');
        if($_GET['type_id']){
            $conditions = 'type_id = '.$_GET['id'];
        }else{
            $conditions = '';
        }
        $page = $this->_get_page();
        $res = $this->_recommend_mod->find(array(
            'conditions' => $conditions,
            'limit' => $page['limit'],
            'count' => true,
            'order' => "sort_order asc"
        )); //找出所有商城的合作伙伴
        $page['item_count'] = $recommend_model->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->assign('order_status_list', array(
            ORDER_PENDING => Lang::get('order_pending'),
            ORDER_SUBMITTED => Lang::get('order_submitted'),
            ORDER_ACCEPTED => Lang::get('order_accepted'),
            ORDER_SHIPPED => Lang::get('order_shipped'),
            ORDER_FINISHED => Lang::get('order_finished'),
            ORDER_CANCELED => Lang::get('order_canceled'),
        ));

        foreach ($res as $key => $ad) {
            $ad['recommend_url'] && $res[$key]['recommend_url'] = dirname(site_url()) . '/' . $ad['recommend_url'];
        }
        $this->assign('recommends', $res);
        $this->assign('filtered', $filter? 1 : 0); //是否有查询条件
        $page['item_count'] = $this->_recommend_mod->getCount();
        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->_format_page($page);
        $this->assign('page_info', $page);
        $this->display('recommend_setting.index.html');
    }

    function add(){
        if (!IS_POST) {

//            $this->assign('recommend_list', $this->get_ad_type_list());
            /* 显示新增表单 */
            $yes_or_no = array(
                1 => Lang::get('yes'),
                0 => Lang::get('no'),
            );
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js'
            ));
            $sgrades = array(
                '1' => '首页recommend轮播图',
                '2' => '商品列表轮播图',
                '3' => '商品分类轮播图',
                '4' => '个人中心轮播图',
            );
            //
            $this->get_goods();
            //
            $this->assign('type', $sgrades);
            $this->assign('yes_or_no', $yes_or_no);
            $this->display('recommend_setting.form.html');

        } else {
            $data = array(
                'goods_id' => $_POST['goods_id'],
                'recommend_title' => $_POST['recommend_title'],
                'sort_order' => $_POST['sort_order'],
            );

            if (!$article_id = $this->_recommend_mod->add($data)) {  //获取article_id
                $this->show_warning($this->_recommend_mod->get_error());
                return;
            } else {
                $this->admin_log("新增recommend[" . $_POST['title'] . "]", 1);
            }

            //处理上传的文章LOGO
            $article_logo = $this->_upload_logo($article_id);
            if ($article_logo === false) {
                return;
            }


            $article_logo && $this->_recommend_mod->edit($article_id, array('recommend_url' => $article_logo)); //将logo地址记下

            $this->show_message('add_recommend_successed', 'back_list', 'index.php?app=recommend_setting', 'continue_add', 'index.php?app=recommend&amp;act=add'
            );
        }


    }

    function edit(){
        $ad_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

        if (!$ad_id)
        {
            $this->show_warning('no_such_recommend');
            return;
        }

        if (!IS_POST)
        {
            $find_data = $this->_recommend_mod->find($ad_id);
            if (empty($find_data))
            {
                $this->show_warning('no_such_recommend');

                return;
            }
            $ad    =   current($find_data);
            if ($ad['recommend_url'])
            {
                $ad['recommend_url']  =   dirname(site_url()) . "/" . $ad['recommend_url'];
            }
//            $this->assign('recommend_list', $this->get_ad_type_list());
            /* 显示新增表单 */
            $yes_or_no = array(
                1 => Lang::get('yes'),
                0 => Lang::get('no'),
            );
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js'
            ));
            $sgrades = array(
                '1' => '首页recommend轮播图',
                '2' => '商品列表轮播图',
                '3' => '商品分类轮播图',
                '4' => '个人中心轮播图',
            );
            if($ad['goods_id']){
                $temp = explode(',',$ad['goods_id']);
                $temp_goods = array();
                foreach ($temp as $k => $v){
                    $temp_goods[$k]['goods_id'] = $v;
                    $c = $this->_goods_mod->get($v);
                    if(is_array($c) && $c){
                        $temp_goods[$k]['goods_name'] = $c['goods_name'];
                        $temp_goods[$k]['default_image'] = dirname(site_url()) . "/" . $c['default_image'];
                    }
                }
                $ad['goods'] = $temp_goods;
            }


            //
            $this->get_goods();
            //
            $this->assign('type', $sgrades);
            $this->assign('yes_or_no', $yes_or_no);
            $this->assign('recommend', $ad);
            $this->display('recommend_setting.form.html');
        }
        else


        {


            $find_data     = $this->_recommend_mod->find($ad_id);
            if (empty($find_data))
            {
                $this->show_warning('no_such_recommend');

                return;
            }

            //删除原图片
            if($_FILES['recommend_url'] && $find_data['recommend_url'] && $find_data['recommend_url'] != $_FILES['recommend_url']){
                if(file_exists(site_url() . '/' . $find_data['recommend_url'])){
                    unlink(site_url() . '/' . $find_data['recommend_url']);
                }
            }

//            site_url()) . '/' . $ad['recommend_url']
            $data = array(
                'goods_id' => $_POST['goods_id'],
                'recommend_title' => $_POST['recommend_title'],
                'sort_order' => $_POST['sort_order'],
            );
            $logo =   $this->_upload_logo($ad_id);

            $logo && $data['recommend_url'] = $logo;
            if ($logo === false)
            {
                return;
            }

            $rows=$this->_recommend_mod->edit($ad_id, $data);
            if ($this->_recommend_mod->has_error())
            {
                $this->show_warning($this->_recommend_mod->get_error());
                return;
            }else{
                $this->admin_log("修改手机广告[".$_POST['ad_name']."]",1);
            }

            $this->show_message('edit_recommend_successed',
                'back_list',        'index.php?app=recommend_setting',
                'edit_again',    'index.php?app=recommend_setting&amp;act=edit&amp;id=' . $ad_id);
        }


    }

    function drop(){
        $article_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
        if (!$article_ids) {
            $this->show_warning('no_such_recommend');

            return;
        }
        $article_ids = explode(',', $article_ids);
        $message = 'drop_ok';
        foreach ($article_ids as $key => $article_id) {
            $article = $this->_recommend_mod->find(intval($article_id));
            //删除原图片
            if($article['recommend_url']){
                if(file_exists(site_url() . '/' . $article['recommend_url'])){
                    unlink(site_url() . '/' . $article['recommend_url']);
                }
            }
        }
        if (!$this->_recommend_mod->drop($article_ids)) {    //删除
            $this->show_warning($this->_recommend_mod->get_error());
            return;
        }else{
            $this->admin_log(Lang::get('del_recommend'), 1);
        }

        $this->show_message($message);

    }

    function get_goods(){
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'goods_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'store_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'brand',
                'equal' => 'like',
            ),
            array(
                'field' => 'closed',
                'type'  => 'int',
            ),
        ));

        // 分类
        $cate_id = empty($_GET['cate_id']) ? 0 : intval($_GET['cate_id']);
        if ($cate_id > 0)
        {
            $cate_mod =& bm('gcategory');
            $cate_ids = $cate_mod->get_descendant_ids($cate_id);
            $conditions .= " AND cate_id" . db_create_in($cate_ids);
        }

        //更新排序
        if (isset($_GET['recommend']) && isset($_GET['order']))
        {
            $recommend  = strtolower(trim($_GET['recommend']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order,array('asc','desc')))
            {
                $recommend  = 'goods_id';
                $order = 'desc';
            }
        }
        else
        {
            $recommend  = 'goods_id';
            $order = 'desc';
        }

        $page = $this->_get_page();
        $goods_list = $this->_goods_mod->get_list(array(
            'conditions' => "1 = 1" . $conditions,
            'count' => true,
            'order' => "$recommend $order",
            'limit' => $page['limit'],
        ));
        foreach ($goods_list as $key => $goods)
        {
            $goods_list[$key]['cate_name'] = $this->_goods_mod->format_cate_name($goods['cate_name']);
        }
        $this->assign('goods_list', $goods_list);

        $page['item_count'] = $this->_goods_mod->getCount();
        $this->_format_page($page);
        $this->assign('page_info', $page);

        // 第一级分类
        $cate_mod =& bm('gcategory', array('_store_id' => 0));
        $this->assign('gcategories', $cate_mod->get_options(0, true));
        $this->import_resource(array('script' => 'mlselection.js,inline_edit.js'));
    }

    function _upload_logo($recommend_id) {
        $file = $_FILES['recommend_url'];
        if ($file['error'] == UPLOAD_ERR_NO_FILE) { // 没有文件被上传
            return '';
        }
        import('uploader.lib');             //导入上传类
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES['recommend_url']); //上传logo
        if (!$uploader->file_info()) {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=recommend_setting&amp;act=edit&amp;id=' . $recommend_id);
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);

        /* 上传 */
        if ($file_path = $uploader->save('data/files/mall/recommend', $recommend_id)) {   //保存到指定目录，并以指定文件名$ad_id存储
            return $file_path;
        } else {
            return false;
        }
    }

    function _upload_detail($recommend_id) {
        $file = $_FILES['detail_url'];
        if ($file['error'] == UPLOAD_ERR_NO_FILE) { // 没有文件被上传
            return '';
        }
        import('uploader.lib');             //导入上传类
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES['detail_url']); //上传logo
        if (!$uploader->file_info()) {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=recommend_setting&amp;act=edit&amp;id=' . $recommend_id);
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);

        /* 上传 */
        if ($file_path = $uploader->save('data/files/mall/detail', $recommend_id)) {   //保存到指定目录，并以指定文件名$ad_id存储
            return $file_path;
        } else {
            return false;
        }
    }
}